using Digitalmes.Application.Scada.Models;
using Digitalmes.Application.Services;
using Digitalmes.Domain.Entities.Mes.Productions;

namespace Digitalmes.Application.Scada.Services.Impl;

/// <summary>
/// 不良品口返工后再上件请求服务。
/// </summary>
internal sealed class ReworkFeedingService(IRepository<ProdCurrentState> currentStateRepo,
    IWorkstationQueryService workstationQueryService) : IReworkFeedingService
{
    public async Task<ScadaResult> FeedingAsync(ReworkFeedingInput input, CancellationToken cancellationToken = default)
    {
        // 检索工位信息
        var workstation = await workstationQueryService.GetWorkstationAsync(input.LineCode, input.WsCode);
        if (workstation == null)
        {
            return ScadaResult.Error(ErrorCode.E1602);
        }

        var isExists = await currentStateRepo.IsAnyAsync(m => m.Sn == input.SN && m.LineId == workstation.LineId, cancellationToken);
        if (!isExists)
        {
            return ScadaResult.Error(ErrorCode.E1603);
        }

        return ScadaResult.Ok();
    }
}
